package cn.wangzg.course1001;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Intent intent;
    private MyService.MyBinder binder;

    private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: ");
            binder= (MyService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
            binder=null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(this, MyService.class);
    }

    //Ctrl+alt+f
    public void btnStart(View view) {

        startService(intent);
    }

    public void btnStop(View view) {
        //Intent intent=new Intent(this,MyService.class);
        stopService(intent);
    }

    public void btnBind(View view) {
        bindService(intent,conn,BIND_AUTO_CREATE);
        binder.getAaa();
    }

    public void btnUnBind(View view) {
        unbindService(conn);
    }
}
